 var map;
 createMap()
 //创建地图
 function createMap(objArr) {
     var _this = this;
     var maphtml = '';
     var mapArray = objArr;
     //初始化地图对象，加载地图
     var map = new AMap.Map("amap", {
         mapStyle: _this.mapStyleCss,
         resizeEnable: true,
         zoom: 8,
         zooms: [3, 15]
         // center: [121.815737, 19.17319]
     });
     _this.map = map
     // map.setCenter([109.51, 18.28]); //设置地图中心点
     //list集合项目
     AMapUI.loadUI(['misc/MarkerList', 'overlay/SimpleMarker', 'overlay/SimpleInfoWindow', 'control/BasicControl'],
         function (MarkerList, SimpleMarker, SimpleInfoWindow, BasicControl) {
             //创建一个实例
             var markerList = new MarkerList({
                 map: map, //关联的map对象
                 listContainer: 'myList', //列表的dom容器的节点或者id, 用于放置getListElement返回的内容

                 getDataId: function (dataItem, index) {
                     //返回数据项的Id
                     return dataItem.id;
                 },
                 getPosition: function (dataItem) {
                     //返回数据项的经纬度，AMap.LngLat实例或者经纬度数组
                     return dataItem.position; //获取所有经纬度重新定位中心点，以及显示各个工地的标点
                     //						return AMap.LngLat(lng: 158.089204, lat: 24.479664, noAutofix: bool);
                 },
                 getMarker: function (dataItem, context, recycledMarker) {
                     var content = context.index;
                     // 创建 AMap.Icon 实例：

                     var label = {
                         offset: new AMap.Pixel(16, 18),
                         content: content
                     };
                     if (recycledMarker) {
                         //存在可回收利用的marker,直接setLabel返回
                         recycledMarker.setLabel(label);
                         return recycledMarker;
                     }
                     if (dataItem.state) {
                         if (dataItem.state == 1) {
                             defaultIconStyle = require('../../assets/images/kanban/1.png')
                         }
                         if (dataItem.state == 2) {
                             defaultIconStyle = require('../../assets/images/kanban/2.png')
                         }
                         if (dataItem.state == 3) {
                             defaultIconStyle = require('../../assets/images/kanban/3.png')
                         }
                     }
                     return new SimpleMarker({
                         // containerClassNames: 'my-marker',
                         iconStyle: defaultIconStyle,
                         iconLabel: label,
                         icon: {
                             size: new AMap.Size(22, 52, ), //图标大小
                         }
                     });
                 },
                 getInfoWindow: function (dataItem, context, recycledInfoWindow) {
                     var tpl = '<p><%- dataItem %><p>';
                     //MarkerList.utils.template支持underscore语法的模板
                     var content = MarkerList.utils.template(tpl, {
                         dataItem: dataItem,
                         dataIndex: context.index
                     });
                     if (recycledInfoWindow) {
                         recycledInfoWindow.setContent(content);
                         return recycledInfoWindow;
                     }
                     //返回一个新的InfoWindow
                     return new AMap.InfoWindow({
                         isCustom: true, //使用自定义窗体
                         offset: new AMap.Pixel(0, -23),
                         content: content
                         // content: createInfoWindow(content.join("<br/>"))
                     });
                 },
                 getListElement: function (dataItem, context, recycledListElement) {

                     var tpl = '<p><%- dataItem %><p>';
                     var content = MarkerList.utils.template(tpl, {
                         dataItem: dataItem,
                         dataIndex: context.index
                     });
                     if (recycledListElement) {
                         //存在可回收利用的listElement, 直接更新内容返回
                         recycledListElement.innerHTML = content;
                         return recycledListElement;
                     }
                     //						lihtml，MarkerList将利用li建一个新的dom节点
                     return '<option>' + content + '</option>';
                 }

             });
             var preMarker = [];
             //监听选中改变
             markerList.on('selectedChanged', function (event, info) {
                 info.selected.data.t = 0;
                 info.selected.data.s = 0;
                 info.selected.data.video = 0;
                 _this.$axios({
                     method: 'GET',
                     url: _this.baseUrl + 'construct/statistics/countByProjectId',
                     params: {
                         projectId: info.selected.data.projectId,
                     },
                     headers: {
                         'Authorization': _this.cookie.get('token')
                     }
                 }).then(res => {
                         if (res.data.status == 'success') {
                             var data = res.data.data;
                             for (var i = 0; i < data.length; i++) {
                                 if (data[i].type == '0') { //塔机
                                     info.selected.data.t = parseInt(data[i].num);
                                     continue;
                                 }
                                 if (data[i].type == '1') { //升降机
                                     info.selected.data.s = parseInt(data[i].num);
                                     continue;
                                 }
                                 if (data[i].type == '2') { //视频监控
                                     info.selected.data.video = parseInt(data[i].num);
                                     continue;
                                 }
                             }
                             maphtml = '';
                             maphtml = _this.mapToolbar(info);
                             if (info.selected) {
                                 if (info.selected.marker) {
                                     //更新为选中样式
                                     info.selected.marker.setIconStyle(selectedIconStyle);
                                 }
                                 var infoWindow = new SimpleInfoWindow({
                                     infoTitle: '<strong style="color:#ddd">' + info.selected.data.projectName +
                                         '</strong>',

                                     infoBody: maphtml,
                                     offset: new AMap.Pixel(0, -31) //基点指向marker的头部位置
                                 });
                                 infoWindow.open(map, info.selected.data.position);
                             }

                             if (info.unSelected && info.unSelected.marker) {
                                 if (info.unSelected.data.state) {
                                     if (info.unSelected.data.state == 1) {
                                         defaultIconStyle = require('../../assets/images/kanban/1.png')
                                     }
                                     if (info.unSelected.data.state == 2) {
                                         defaultIconStyle = require('../../assets/images/kanban/2.png')
                                     }
                                     if (info.unSelected.data.state == 3) {
                                         defaultIconStyle = require('../../assets/images/kanban/3.png')
                                     }
                                     //更新为默认样式
                                     info.unSelected.marker.setIconStyle(defaultIconStyle);
                                 }
                             }
                         }
                     },
                     error => {
                         _this.$message("数据获取失败！");
                         console.log(error)

                     }
                 );
                 if (maphtml == '') {
                     maphtml = _this.mapToolbar(info);
                 } else {
                     maphtml = '';
                     maphtml = _this.mapToolbar(info);
                 }

                 if (info.selected) {
                     if (info.selected.marker) {
                         //更新为选中样式
                         info.selected.marker.setIconStyle(selectedIconStyle);
                     }
                     var infoWindow = new SimpleInfoWindow({
                         infoTitle: '<strong style="color:#ddd">' + info.selected.data.projectName + '</strong>',
                         infoBody: maphtml,
                         offset: new AMap.Pixel(0, -31) //基点指向marker的头部位置
                     });
                     infoWindow.open(map, info.selected.data.position);
                 }
                 if (info.unSelected && info.unSelected.marker) {
                     //更新为默认样式
                     info.unSelected.marker.setIconStyle(defaultIconStyle);
                 }


             });
             //加载地图数据
             var arrayData = [];

             //加载数据赋值
             mapArray.forEach(function (obj, index, array) {
                 if (obj.longitude) {
                     arrayData.push({
                         pid: obj.projectId,
                         position: ([obj.longitude, obj.latitude]),
                         projectName: obj.projectName ? obj.projectName : "",
                         projectId: obj.projectId ? obj.projectId : "",
                         address: obj.address ? obj.address : "",
                         emEntName: obj.consCorpName ? obj.consCorpName : "",
                         state: obj.state ? obj.state : "",
                     })
                 }
             })
             markerList.render(arrayData);
             //清除数据
             // markerList.render([]);
             // //缩放控件
             map.addControl(new BasicControl.Zoom({
                 position: 'rt',
                 showZoomNum: true,
                 theme: 'my-style'
             }))
         });
 }